import { Component, Input, OnInit } from '@angular/core';

import { pb } from 'src/pb';
import { SettingsService } from '../services/settings.service';

@Component({
  selector: 'app-file',
  templateUrl: './file.component.html',
  styleUrls: ['./file.component.scss'],
})
export class FileComponent implements OnInit {
  @Input()
  file: pb.IFileInfo;
  constructor(
    public ss: SettingsService,
  ) { }

  link() {
    if (this.file.isDir) {
      return `/folder/${this.file.id}`;
    }

    return `/view/${this.file.id}`;
  }

  get disabled() {
    if (this.file.isDir) {
      return false;
    }

    switch (this.file.type) {
      case pb.FileType.text:
      case pb.FileType.code:
        if (this.file.size > this.ss.config.maxFileSize) {
          return true;
        }
      // eslint-disable-next-line no-fallthrough
      case pb.FileType.image:
      case pb.FileType.pdf:
      case pb.FileType.audio:
      case pb.FileType.video:
        // case pb.FileType.document:
        return false;
    }

    return true;
  }

  get icon() {
    if (this.file.isDir) {
      if (pb.FileType.archive) {
        return 'folder-open';
      }

      return 'folder-sharp';
    }

    switch (this.file.type) {
      case pb.FileType.code:
        return 'code-slash';
      case pb.FileType.text:
        return 'reader';
      case pb.FileType.image:
        return 'image';
      case pb.FileType.video:
        return 'tv';
      case pb.FileType.audio:
        return 'volume-high';
      case pb.FileType.pdf:
        return 'receipt';
      case pb.FileType.archive:
        return 'folder-open';
      case pb.FileType.document:
        return 'document-text';
    }

    return 'document';
  }

  get showImage() {
    return this.file.type === pb.FileType.image;
  }

  get url() {
    return `/api/file/original/${this.file.id}`;
  }

  ngOnInit() { }

}
